package com.zwj.batch.config.test;

import com.zwj.batch.config.springBatch.incrementer.DailyTimestampParamIncrementer;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 作业时间戳参数
 *
 * @author 张文接
 * @version 1.0
 * @createTime 2023-05-22 21:28:00
 */
//@Configuration
public class BatchConfig05 {
    //job构造工厂
    @Resource
    private JobBuilderFactory jobBuilderFactory;
    //step构造工厂
    @Resource
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 逻辑主体
     *
     * @return
     */
    @StepScope
    @Bean
    public Tasklet tasklet() {
        return (stepContribution, chunkContext) -> {
            Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
            System.out.println(jobParameters.get("name") + "=======" + jobParameters.get("daily"));
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("incr-params-job")//给job命名
                .start(step1())//具体执行什么步骤(step)
                .incrementer(dailyTimestampParamIncrementer())//保证参数自增，可以让一个job重复执行
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory
                .get("incr-params-step")//给step命名
                .tasklet(tasklet())//注入具体要执行的逻辑
                .build();//构建
    }

    @Bean
    public DailyTimestampParamIncrementer dailyTimestampParamIncrementer() {
        return new DailyTimestampParamIncrementer();
    }
}
